﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using VirtuOS.Base;
using System.IO;

namespace VirtuOS.Components.Commands
{
    class set
    {
        public static string help = "Sets environmental variables.";
        public static string register_name = "set";
        public static bool requires_root = false;

        public static void Command(string[] args)
        {
            if ((args.Length > 0) && (Regex.Match(args[0], @"(-)?-help", RegexOptions.IgnoreCase).Success))
            {
                Console.WriteLine(help);
            }
            else
            {
                if ((args.Length > 0) && (args[0] != null))
                {
                    string cd = FileHandler.CurrentDirectory();
                    Terminal.ChangeDir("/");
                    string tmp = string.Empty;
                    foreach (string s in args)
                        tmp = tmp + " " + s;
                    tmp = tmp.Trim();
                    string[] parse = tmp.Split('=');
                    string v = parse[0];
                    string value = tmp.Replace(v + "=", "");
                    Match m = Regex.Match(value, "\".*\"", RegexOptions.IgnoreCase);
                    if (m.Success)
                        value = m.Groups[0].Value;
                    else
                    {
                        string[] s = value.Split(' ');
                        value = s[0];
                    }
                    if (!value.StartsWith("\"")) value = "\"" + value;
                    if (!value.EndsWith("\"")) value = value + "\"";
                    if (!File.Exists("var/environment.var"))
                        FileHandler.MakeFile("var/environment.var");
                    bool replace = false;
                    int l;
                    if (FileHandler.CountLinesInFile("var/environment.var") == 0) { l = 1; }
                    else { l = Convert.ToInt32(FileHandler.CountLinesInFile("var/environment.var")); }
                    string[] temp = new string[l];
                    int le = 0;
                    using (StreamReader sr = new StreamReader("var/environment.var"))
                    {
                        while (!sr.EndOfStream)
                        {
                            string cl = sr.ReadLine();
                            if ((cl.Length > 0) && (!cl.StartsWith(" ")))
                            {
                                string[] st = cl.Split('=');
                                if (st[0].Equals(v))
                                    replace = true;
                                temp[le] = cl;
                                le++;
                            }
                        }
                    }

                    for (int i = 0; i < temp.Length; i++)
                    {
                        string[] sp = temp[i].Split('=');
                        if (sp[0].Equals(v))
                            temp[i] = v + "=" + value;
                    }

                    if (replace == false)
                    {
                        string[] wr = new string[temp.Length  + 1];
                        for (int i = 0; i < wr.Length; i++)
                        {
                            if (i == (wr.Length - 1))
                                wr[i] = v + "=" + value;
                            else
                                wr[i] = temp[i];
                        }
                        temp = wr;
                    }
                    using (StreamWriter sw = new StreamWriter("var/environment.var"))
                    {
                        foreach (string s in temp)
                        {
                            string[] st = s.Split('=');
                            if (!st[1].Equals("\"\""))
                            {
                                sw.WriteLine(s);
                            }
                        }
                    }
                    Terminal.ChangeDir(cd);
                }
            }
        }
    }
}
